<template>
  <v-container class="d-flex flex-column align-center justify-center" style="height: 100vh;">
    <v-card width="400">
      <v-card-title class="text-h5">管理员登录</v-card-title>
      <v-card-text>
        <v-text-field v-model="username" label="账号" required></v-text-field>
        <v-text-field v-model="password" label="密码" type="password" required></v-text-field>
        <v-alert v-if="error" type="error" dense>{{ error }}</v-alert>
      </v-card-text>
      <v-card-actions>
        <v-btn color="primary" @click="login" :loading="loading" block>登录</v-btn>
      </v-card-actions>
    </v-card>
  </v-container>
</template>

<script>
import api from '../services/api.js'
export default {
  name: 'AdminLogin',
  data() {
    return {
      username: '',
      password: '',
      error: '',
      loading: false
    }
  },
  methods: {
    async login() {
      this.loading = true
      this.error = ''
      try {
        const res = await api.adminLogin(this.username, this.password)
        if (res.data.success) {
          this.$router.push('/admin')
        } else {
          this.error = res.data.message || '登录失败'
        }
      } catch (e) {
        this.error = '网络错误'
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
